package com.kaifamiao.servlet;

import com.kaifamiao.entity.Customer;
import com.kaifamiao.jdbc.helper.JdbcHelper;
import com.kaifamiao.jdbc.helper.RowHolder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.util.List;

@WebServlet("/customer/list")
public class CustomerListServlet extends HttpServlet {

    private JdbcHelper helper;
    private RowHolder<Customer> holder;
    private String select;

    @Override
    public void init() throws ServletException {
        helper = JdbcHelper.getInstance();
        holder = rs -> {
            Customer c = new Customer();
            c.setId( rs.getInt( "id" ) );
            c.setUsername( rs.getString( "username" ) );
            c.setNickname( rs.getString( "nickname" ) );
            java.sql.Date date = rs.getDate( "reg_date" );
            LocalDate regDate = date == null ? null : date.toLocalDate();
            c.setRegistrationDate( regDate );
            return c ;
        };
        select = "SELECT id, username, nickname, reg_date FROM t_customers";
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding( "UTF-8" );
        response.setContentType( "text/html; charset=UTF-8" );
        PrintWriter out = response.getWriter();
        out.println( "<link rel=\"stylesheet\" href=\"/css/customer.css\">");
        List<Customer> list = helper.query( select, holder);
        if( list != null && list.size() > 0 ) {
            out.println( "<table>" );
            out.println( "<tr>");
            out.println( "<td>序号</td>");
            out.println( "<td>用户名</td>");
            out.println( "<td>昵称</td>");
            out.println( "<td>注册日期</td>");
            out.println( "<td></td>");
            out.println( "</tr>");
            for (int i = 0, n = list.size(); i < n; i++) {
                Customer x = list.get(i);
                out.println( "<tr>");
                out.println( "<td>" + ( i + 1 ) + "</td>");
                out.println( "<td>" + x.getUsername() + "</td>");
                out.println( "<td>" + x.getNickname() + "</td>");
                out.println( "<td>" + x.getRegistrationDate() + "</td>");
                out.println( "<td>");
                out.println( "<a href=\"/customer/detail/page?id=" + x.getId() + "\">详情</a>");
                out.println( "<a href=\"/customer/edit/page?id=" + x.getId() + "\">编辑</a>");
                out.println( "<a href=\"/customer/remove?id=" + x.getId() + "\">删除</a>");
                out.println( "</td>");
                out.println( "</tr>");
            }
            out.println( "</table>" );
        }
        out.println( "<p>");
        out.println( "<a href=\"/customer/add/page\">添加新用户</a>");
        out.println( "</p>");
    }

    @Override
    public void destroy() {
        helper.release();
    }
}
